Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End help with foreach loop

  • This topic is empty.
Viewing 15 posts - 1 through 15 (of 15 total)
  • Author
    Posts
  • #156358
    cybershot
    Participant

    I am working in wordpress. I have a variable that is checking for templates. When I use print_r( $template ) I get a this

    template-discography.php template-page.php template-news.php template-shows.php template-videos.php template-band.php template-images.php default page-templates/front-page.php template-page.php template-contact.php default template-twitter.php template-widgets.php template-test.php
    

    I wanted to include all these templates into my site so I did a foreach loop like so

    foreach( $template as $tempname ) {
            include( $tempname );
     }
    

    I am getting an error saying invalid argument in foreach. Not sure what the issue is.

    #156364
    chrisburton
    Participant

    I don’t understand what you’re trying to do. Can you elaborate?

    #156367
    cybershot
    Participant

    I have been trying for over a year to make a one page wordpress theme without using ajax. I figured there is a way to get the content without using ajax. I got it working but it’s not the greatest. I found that the pages ignore the page templates. So if you make a page and assign a page template, the page will come up blank. The only way I have been able to get it working is to make a shortcode and put the page query into that and put the shortcode into the page with a blank template. So I have my query just checking to see if the page has assigned a page template and if it does to run the content. I am trying to find another way. A better way that doesn’t need shortcodes to make it work. So I have been messing with including the page templates. I might have already tried this but not sure. So I have a custom loop and now I am trying to get the page templates. inside the loop, I have this

    $template = get_post_meta( get_the_ID(), '_wp_page_template', TRUE );
    

    That will get all the page templates. So I tried a foreach loop as shown above to include them.

    #156369
    chrisburton
    Participant

    How are you calling each template when you want to change content?

    #156370
    cybershot
    Participant

    each template uses categories to get it’s content. When I make a template called template-contact.php and I put the contact page specific code into that template and then make a page called contact and assign the contact page template to that page, the page will be blank when I go to it. The only way I could get the actual content to show was to paste all the code into functions.php as a shortcode and put that shortcode into the page. It appears that the template structure gets ignored by this method. Here is the full code to my custom query. maybe this will make it make more sense. I will put comments in the code to explain it.

     //starting my custom query 
        $page_query = new WP_Query(array(
        'post_status' => 'publish',
        'post_type' => 'page',   // getting the pages
        'order' => 'ASC',
        'orderby' => 'menu_order',
        'posts_per_page' => '-1',
        'offset' => '1',
        'paged' => (get_query_var('paged')) ? get_query_var('paged') : 1
    ));
    
            if ( $page_query->have_posts() ) : while ( $page_query->have_posts() ) : $page_query->the_post(); ?>
    
                         //checking to see if the page has a template assigned to it
            <?php $template = get_post_meta( get_the_ID(), '_wp_page_template', TRUE ); ?> 
    
              //asking the system not to load a page that doesn't have any content in it.  This prevents duplicates
            <?php if( $template && $post->post_content != '' ) { ?>
    
                                               The theme has a jquery powered slider that slides each page into view. This list element is      programmed to hold all the pages. The id="page_page-title" interacts with the code to slide the page
                        <li id="page_<?php echo str_replace(' ','_',strtolower( get_the_title())); ?>">
    
                                                        //using the standard WordPress code to get the page
                            <?php get_template_part( 'content', 'page' ); ?>
                        </li>
             <?php } 
    
            endwhile;
            endif;
    }
    
    #156375
    chrisburton
    Participant

    I don’t think I can be much help here but out of curiosity, why are you doing it this way? Seems like a more complicated way of building a website.

    #156376
    cybershot
    Participant

    because the site is a one page WordPress site. There are many ways of doing I think. This is just the way I tried it and got it working. Are there better ways? I am sure there are but I don’t know them and that is what I am looking for.

    #156382
    __
    Participant

    I don’t know about wordpress, so I couldn’t offer much advice on that front.

    In fairness, I don’t quite understand what your objective is. However, the fact that you’ve been trying for so long without success would seem to indicate that it’s simply not something WP was designed to do, or is well-suited for. WP is a mature platform, with a well-established way of doing things. It is intended to be used in a particular way. It’s not really a proving ground for new ideas.

    I can say that, if print_r( $template ) results in

    template-discography.php template-page.php template-news.php template-shows.php template-videos.php template-band.php template-images.php default page-templates/front-page.php template-page.php template-contact.php default template-twitter.php template-widgets.php template-test.php
    

    then $template is a string. foreach requires an array or iterable object, which is why you have the “invalid argument” error.

    You could split that string on the spaces to create an array:

    $array_of_templates = explode( ' ',$template );
    
    #156432
    Alen
    Participant

    If you are building single page website why are you creating pages? I don’t get it.

    #156469
    cybershot
    Participant

    because I am working in WordPress. Pages are not posts. WordPress is set up to only get posts for the front page. So in order to get all pages as well. I need a query that will get all the pages as well as posts into the front page. Doing this is difficult as far as I am concerned. I have been trying for over a year to figure this out without using ajax. it’s tricky. I have many different ways of doing it just with different results. Still just looking for the best result

    #156474
    chrisburton
    Participant

    Is there a reason you don’t want to use AJAX?

    #156476
    cybershot
    Participant

    I can’t find a script that would work well for it. I tried ajax once but couldn’t get it to work.

    #156478
    Alen
    Participant

    Pages are not posts

    Really?

    You are building single page website. Right? So why do you need pages?

    You can set up theme options and create fields for updating whatever info you need. Then you can have one template file that pulls in all the data. Depending on how complex you need this to be… WordPress might not be the right solution.

    #156480
    cybershot
    Participant

    I need pages because WordPress has the ability to create them. So I am adding support for them. Yes, pages are not posts. They act differently then posts. That information is in the codex. If you pull a page into the frontpage for a single page site. then it will act as a post and not a page. That would be bad. Right now I have it set up so that you can still make pages and that page will get pulled into the front page. As I also stated. There are several ways to do this. I have tried many different ways. I do have this working. Just looking for a better way. If you have never made a one page WordPress site. I would suggest you try it before you pick my method apart to much. I don’t think it’s as straight forward as you think it is. Take a look at the theme I am making

    http://www.rc-hc.com/mafloral/

    #156481
    Alen
    Participant

    I don’t think it’s as straight forward as you think it is.

    I guess not.

Viewing 15 posts - 1 through 15 (of 15 total)
  • The forum ‘Back End’ is closed to new topics and replies.