• Hi guys!

    Im looking a little bit of help.
    I want to write out this in my query <div id=”nyhetsbild”><?php images(‘1’, ‘170’, ”, ‘alignleft’, true); ?> if the post have an images. But if not, i don’t want the div at all.
    Could someone help me?

    This is my code:

    <div id=”content”><span class=”contentrubbe”>Senaste nyheter</span>

    <?php query_posts(‘cat=3&showposts=10’); ?>
    <?php while (have_posts()) : the_post(); ?>
    <div id=”contentpost”>
    <span class=”contentrubbe4″><?php the_title(); ?></span>
    <div id=”nyhetsbild”><?php images(‘1’, ‘170’, ”, ‘alignleft’, true); ?></div>
    <div id=”nyhetstext”><span class=”newsrolldate”><?php the_time(‘j M, Y’) ?></span>
    <?php content(30); ?>… “>Läs artikeln </div>
    <div id=”clear”></div></div>
    <?php endwhile; ?></div>

Viewing 4 replies - 1 through 4 (of 4 total)
  • please mark your code with the ‘code’ button (highlight the code and press the ‘code’ button once; or click the ‘code’ button, write/paste the code, and then click the ‘code’ button again) or use backticks (on an english keyboard top left, beside the number 1) to keep it readable.

    images('1', '170', '', 'alignleft', true); does not seem to be a default wordpress function;

    deducting from similar functions, you could try:

    <?php if( images('1', '170', '', 'alignleft', false) ) { ?>
    <div id="nyhetsbild"><?php images('1', '170', '', 'alignleft', true); ?></div>
    <?php } ?>

    if this does not work, try to identify the images() function, possibly in functions.php, and see if there is a possibility to use it in a conditional statement.

    Thread Starter lobaat

    (@lobaat)

    Oh thanks!
    This is the code that allows me to break the content apart (from theme functions.php):

    // Content Limit
    	function content($num, $more_link_text = '(more...)') {
    	$theContent = get_the_content($more_link_text);
    	$output = preg_replace('/<img[^>]+./','', $theContent);
    	$limit = $num+1;
    	$content = explode(' ', $output, $limit);
    	array_pop($content);
    	$content = implode(" ",$content);
        $content = strip_tags($content, '<p><a><address><a><abbr><acronym><b><big><blockquote><br><caption><cite><class><code><col><del><dd><div><dl><dt><em><font><h1><h2><h3><h4><h5><h6><hr><i><img><ins><kbd><li><ol><p><pre><q><s><span><strike><strong><sub><sup><table><tbody><td><tfoot><tr><tt><ul><var>');
          echo close_tags($content);
    }
    function images($num = 1, $width = null, $height = null, $class = 'alignleft', $displayLink = true) {
    	global $more;
    	$more = 1;
    	if($width) { $size = ' width="'.$width.'px"'; }
    	if($height) { $size .= ' height="'.$height.'px"'; }
    	if($class) { $class = ' class="'.$class.'"'; }
    	if($displayLink != false) { $link = '<a href="'.get_permalink().'">'; $linkend = '</a>'; }
    	$content = get_the_content();
    	$count = substr_count($content, '<img');
    	$start = 0;
    	for($i=1;$i<=$count;$i++) {
    		$imgBeg = strpos($content, '<img', $start);
    		$post = substr($content, $imgBeg);
    		$imgEnd = strpos($post, '>');
    		$postOutput = substr($post, 0, $imgEnd+1);
    		if($width || $height) {
    			$replace = array('/width="[^"]*" /','/height="[^"]*" /','/class="[^"]*" /');
    		} else {
    			$replace = '/class="[^"]*" /';
    		}
    		$postOutput = preg_replace($replace, '',$postOutput);
    		$image[$i] = $postOutput;
    		$start=$imgBeg+$imgEnd+1;
    	}
    	if($num == 'all') {
    		$x = count($image);
    		for($i = 1;$i<=$x; $i++) {
    			if(stristr($image[$i],'<img')) {
    			$theImage = str_replace('<img', '<img'.$size.$class, $image[$i]);
    			echo $link.$theImage.$linkend;
    			}
    		}
    	} else {
    		if(stristr($image[$num],'<img')) {
    			$theImage = str_replace('<img', '<img'.$size.$class, $image[$num]);
    			echo $link.$theImage.$linkend;
    		}
    	}
    	$more = 0;
    }?>
    Thread Starter lobaat

    (@lobaat)

    So what I need to find out is IF the post contains a image write my div else write noting….

    Think you could help?
    Many thanks

    [next time, please use the pastebin when posting large amounts of code.]

    this might work –
    it is basically copying some of the code that you posted:

    <?php if( strpos(get_the_content(), '<img ') >=1 ) { ?>
    <div id="nyhetsbild"><?php images('1', '170', '', 'alignleft', true); ?></div>
    <?php } ?>
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Hellp with a if’ is closed to new replies.