WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Single quotes within single quotes (PHP) (6 posts)

  1. Marc
    Member
    Posted 3 years ago #

    I'm attempting to make the following line of code work:

    <?php $temp = $wp_query; $wp_query= null; $wp_query = new WP_Query(); $wp_query->query('showposts=5'.'&paged='.$paged); query_posts('category_name=
    	<?php
    	global $wp_query;
    	$postid = $wp_query->post->ID;
    	echo get_post_meta($postid, <strong>'BlogCat'</strong>, true);
    	?>
    '.'&paged='.$paged);
    ?>

    The BlogCat contains single quotes which are cancelling the other quotes and causing a syntax error. Cancelling the quotes (with a \)stops the error but the custom field is not called up. How do I fix this?

  2. esmi
    Forum Moderator
    Posted 3 years ago #

    <?php $my_meta = "<strong>'BlogCat'</strong>"'
    $temp = $wp_query; $wp_query= null; $wp_query = new WP_Query(); $wp_query->query('showposts=5'.'&paged='.$paged); query_posts('category_name=
    	<?php
    	global $wp_query;
    	$postid = $wp_query->post->ID;
    	echo get_post_meta($postid, $my_meta, true);
    	?>
    '.'&paged='.$paged);
    ?>
  3. Marc
    Member
    Posted 3 years ago #

    Using that gives me the following error:

    Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in /home/content/06/5371606/html/wp-content/themes/Srizzil2/blog.php on line 36

  4. esmi
    Forum Moderator
    Posted 3 years ago #

    I'm not sure what you thought you were doing with the code but I thought it looked pretty mangled. There's no category name being passed to the query; an extra opening <?php tag in the middle of the query arguments and other bits that make no sense at all. Best I can do is create 2 code snippets from it:

    <?php
    $temp = $wp_query;
    $wp_query = new WP_Query(); $wp_query->query('showposts=5'.'&paged='.$paged); ?>

    and:

    <?php
    $postid = $wp_query->post->ID;
    $my_meta = "<strong>'BlogCat'</strong>";
    echo get_post_meta($postid, $my_meta, true);
    ?>
  5. alchymyth
    The Sweeper & Moderator
    Posted 3 years ago #

    you initial code is already totally broken:
    there is all this php meta stuff in the middle of query_posts() parameters;
    and you should either use WP_Query() or query_posts();

    try:

    <?php 	global $wp_query;
    	$postid = $wp_query->post->ID;
    	$blogcat = get_post_meta($postid, 'BlogCat', true);
    	?>
    $temp = $wp_query; $wp_query= null; $wp_query = new WP_Query(); $wp_query->query('posts_per_page=5&paged='.$paged . '&category_name='.$blogcat);
    ?>
  6. Marc
    Member
    Posted 3 years ago #

    Fantastic, that did the trick.

    What I am trying to do is make the template look for the category to include using the custom field. This way I can use one template for multiple pages with more focussed categories.

    There was a slight mistake, but I noticed it and corrected it. Here is the final working code.

    <?php 	global $wp_query;
    $postid = $wp_query->post->ID;
    $blogcat = get_post_meta($postid, 'BlogCat', true);
    $temp = $wp_query; $wp_query= null; $wp_query = new WP_Query(); $wp_query->query('posts_per_page=5&paged='.$paged . '&category_name='.$blogcat);
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic